import os
import sys
import django

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.extend([BASE_DIR, ])
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "finance_c2c.settings")
django.setup()
from apps.order.models import PriceUpdate
from apps.product.models import Product
from apps.sales_man.models import ChannelProduct
from core.tools.mysql_connect import SqlModel


class InsertPrice(object):
    def __init__(self, channel, data_price):
        self.channel = channel
        self.data_price = data_price

    def get_product(self, product_id):
        return Product.objects.get(id=product_id)

    def get_child_product(self, product_id):
        product_obj = self.get_product(product_id)
        return product_obj.product_set.all()

    def get_channel_product(self, product_id):
        """
        parent
        :return:
        """
        return ChannelProduct.objects.get(channel_id=self.channel, product_id=product_id)

    def get_parent_price(self, product_id):
        channel_product = self.get_channel_product(product_id)
        return PriceUpdate.objects.filter(channel_product=channel_product).order_by("-id").first()

    def create_data(self, product_id):
        child_product_list = self.get_child_product(product_id)
        parent_price = self.get_parent_price(product_id)
        for i in child_product_list:
            # child
            channel_product_obj = ChannelProduct.objects.get(channel_id=self.channel, product_id=i)
            PriceUpdate.objects.get_or_create(
                channel_product=channel_product_obj,
                sum_price=parent_price.sum_price,
                reserved=parent_price.reserved,
                level1=parent_price.level1,
                level2=parent_price.level2,
                level3=parent_price.level3,
                price_type=parent_price.price_type
            )

    def get_channel_products(self):
        """
        获取此渠道的产品
        :return:
        """
        return ChannelProduct.objects.filter(channle=self.channel)

    def insert_price(self):
        products = ChannelProduct.objects.filter(channel_id=self.channel)
        products = products.filter(product__parent_product__isnull=True)
        """
        浦发：22
        交通： 3
        上海： 24
        光大： 15
        兴业： 	8
        华夏： 	35
        民生： 	23
        中原： 	12
        车抵贷： 	17
        天津浦发： 	54
        :return:
        """
        for i in products:
            channel_product_obj = ChannelProduct.objects.get(channel_id=self.channel, product=i.product)
            for price in self.data_price:
                if i.product.id == price.get("product_id"):
                    if price.get("price_type"):
                        price_type = price.get("price_type")
                    else:
                        price_type = 1
                    PriceUpdate.objects.get_or_create(
                        sum_price=price.get("sum_price"),
                        level1=price.get("level1"),
                        level2=price.get("level2"),
                        level3=price.get("level3"),
                        reserved=price.get("reserved"),
                        channel_product=channel_product_obj,
                        price_type=price_type,
                    )


if __name__ == '__main__':
    # 正式
    # data = [
    #     # {"product_id": 54, "price1": 0, "price2": 31.5, "price3": 31.5},  # 浦发 天津
    #     {"product_id": 22, "price1": 66, "price2": 27.5, "price3": 16.5},  # 浦发
    #     {"product_id": 3, "price1": 60, "price2": 25, "price3": 15},  # 交通
    #     {"product_id": 15, "price1": 60, "price2": 25, "price3": 15},  # 光大
    #     {"product_id": 24, "price1": 60, "price2": 27.5, "price3": 16.5},  # 上海
    #     {"product_id": 8, "price1": 46, "price2": 16, "price3": 3},  # 兴业
    #     {"product_id": 35, "price1": 35, "price2": 13, "price3": 3},  # 华夏
    #     {"product_id": 23, "price1": 35, "price2": 13, "price3": 3},  # 民生
    #     {"product_id": 12, "price1": 0.0053, "price2": 0.0019, "price3": 0.0004, "price_type": 2},  # 中原
    #     {"product_id": 17, "price1": 0.0053, "price2": 0.0019, "price3": 0.0004, "price_type": 2},  # 车抵贷
    # ]

    channel_id = 23
    data = [
        # {"sum_price": 45, "reserved": 0, "product_id": 54, "level1": 0, "level2": 0.7222, "level3": 0.2778},  # 浦发 天津
        {"sum_price": 110, "reserved": 10, "product_id": 22, "level1": 0.0, "level2": 0.6818, "level3": 0.3182},  # 浦发
        {"sum_price": 100, "reserved": 10, "product_id": 3, "level1": 0.0, "level2": 0.7, "level3": 0.3},  # 交通
        {"sum_price": 75, "reserved": 10, "product_id": 15, "level1": 0.0, "level2": 0.7, "level3": 0.3},  # 光大
        {"sum_price": 110, "reserved": 10, "product_id": 24, "level1": 0.0, "level2": 0.6818, "level3": 0.3182},  # 上海
        # {"sum_price": 75, "reserved": 0, "product_id": 8, "level1": 0.0, "level2": 0.8667, "level3": 0.1333},  # 兴业
        # {"sum_price": 60, "reserved": 0, "product_id": 23, "level1": 0.0, "level2": 0.8333, "level3": 0.1667},  # 民生
        # {"sum_price": 60, "reserved": 0.0, "product_id": 35, "level1": 0.0, "level2": 0.8333, "level3": 0.1667},  # 华夏
        # {"sum_price": 0.0095, "reserved": 0.0, "product_id": 12, "level1": 0.0, "level2": 0.5789, "level3": 0.4211,
        #  "price_type": 2},  # 中原
        # {"sum_price": 0.0095, "reserved": 0.0, "product_id": 17, "level1": 0.0, "level2": 0.5789, "level3": 0.4211,
        #  "price_type": 2},  # 车抵贷
    ]
    insert_ = InsertPrice(channel_id, data)
    insert_.insert_price()
    # insert_.create_data(24)  # 上海银行
    # insert_.create_data(35)  # 华夏
